package com.wz.project.mydo.mydojobdetail.controller;

import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.mydo.domain.MydoJobDetail;
import com.wz.project.mydo.mydojob.service.IMydoJobService;
import com.wz.project.mydo.mydojobdetail.service.IJobDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业详情 信息操作处理
 * 
 * @author sys
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/mydo/jobdetail")
public class JobDetailController extends BaseController
{
    private String prefix = "mydo/jobdetail";
	
	@Autowired
	private IJobDetailService jobDetailService;

	@Autowired
	private IMydoJobService mydoJobService;
	
	@RequiresPermissions("mydo:jobdetail:view")
	@GetMapping()
	public String jobDetail()
	{
	    return prefix + "/jobdetail";
	}
	
	/**
	 * 查询作业详情列表
	 */
//	@RequiresPermissions("mydo:jobdetail:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(MydoJobDetail jobDetail)
	{
		startPage();
		List<MydoJobDetail> list = null;
        list = jobDetailService.selectJobDetailList(jobDetail);
        if (list.size() == 0){
			list = jobDetailService.selectJobDetailListFinish(jobDetail.getJobId());
		}
		return getDataTable(list);
	}
	
	/**
	 * 新增作业详情
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存作业详情
	 */
	@RequiresPermissions("mydo:jobdetail:add")
	@Log(title = "作业详情", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MydoJobDetail jobDetail)
	{		
		return toAjax(jobDetailService.insertJobDetail(jobDetail));
	}

	/**
	 * 修改作业详情
	 */
	@GetMapping("/edit{id}")
	public String edit(@PathVariable("id") String jobDetailId, ModelMap mmap)
	{
		MydoJobDetail jobDetail = null;
		jobDetail = jobDetailService.selectJobDetailById(jobDetailId);
		if (jobDetail == null){
			jobDetail = jobDetailService.selectJobDetailFinishById(jobDetailId);
		}
		mmap.put("jobDetail", jobDetail);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存作业详情
	 */
//	@RequiresPermissions("mydo:jobdetail:edit")
	@Log(title = "作业详情", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MydoJobDetail jobDetail)
	{		
		return toAjax(jobDetailService.updateJobDetail(jobDetail));
	}
	
	/**
	 * 删除作业详情
	 */
//	@RequiresPermissions("mydo:jobdetail:remove")
	@Log(title = "作业详情", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(jobDetailService.deleteJobDetailByIds(ids));
	}

	/**
	 * 删除作业详情
	 */
//	@RequiresPermissions("mydo:jobdetail:remove")
	@Log(title = "作业详情", businessType = BusinessType.DELETE)
	@RequestMapping("/removel")
	@ResponseBody
	public AjaxResult removel(String ids)
	{
		MydoJobDetail mydoJobDetail = jobDetailService.selectJobDetailByIds(ids);
		if (mydoJobDetail == null){
			return AjaxResult.error("只能删除未执行/正执行的子作业");
		}
		MydoJobDetail mydoJobDetail1 = new MydoJobDetail();
		mydoJobDetail1.setJobId(mydoJobDetail.getJobId());
		List<MydoJobDetail> mydoJobDetails = jobDetailService.selectJobDetailList(mydoJobDetail1);
		int i = jobDetailService.deleteJobDetailByIds(ids);
		if (i == 1 && mydoJobDetails.size()==1){
			mydoJobService.deleteJobByIds(mydoJobDetail.getJobId());
		}

		return toAjax(i);
	}


}
